﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common.Protocol;

namespace UI
{
    public partial class Chart : UserControl
    {
        #region private fields

        private DateTime start, end;

        #endregion

        public Chart()
        {
            this.InitializeComponent();
            
            this.StartTime = DateTime.Now;
            this.EndTime = DateTime.Now.AddHours(6);

            this.chartCanvas1 = new ChartCanvas(this);
            this.elementHost1.Child = this.chartCanvas1;
        }
        
        #region dates properties

        public DateTime StartTime
        {
            get
            {
                return this.start;
            }

            set
            {
                this.start = value;
                if (this.end != null && this.start != null)
                {
                    this.CurrentTime = this.StartTime.AddMilliseconds((this.EndTime - this.StartTime).TotalMilliseconds / 2.0);
                }

                if (this.chartCanvas1 != null)
                {
                    this.chartCanvas1.InvalidateVisual();
                }
            }
        }

        public DateTime EndTime
        {
            get
            {
                return this.end;
            }

            set
            {
                this.end = value;
                if (this.start != null && this.end != null)
                {
                    this.CurrentTime = this.StartTime.AddMilliseconds((this.EndTime - this.StartTime).TotalMilliseconds / 2.0);
                }

                if (this.chartCanvas1 != null)
                {
                    this.chartCanvas1.InvalidateVisual();
                }
            }
        }

        public DateTime CurrentTime { get; private set; }

        #endregion

        public void Initialize(List<ProtocolInterval> data, long totalMilliseconds)
        {
            if (data.Count > 0)
            {
                //this.StartTime = data[0].Start;
                //this.EndTime = data[0].End;
            }

            this.chartCanvas1.SetChannelData(data, totalMilliseconds);
        }

        public void NavigateToTime(DateTime time)
        {
            
        }
    }
}